package com.honey.compilation.sql.dml;

import java.util.ArrayList;
import java.util.List;

import com.honey.compilation.sql.SqlColumn;
import com.honey.compilation.sql.SqlKeyWord;
import com.honey.compilation.sql.SqlScript;
import com.honey.compilation.sql.SqlTable;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.IndentSpace;

/**
 * 插入数据 INSERT INTO
 * @author Administrator
 *
 */
public  class Insert extends AbstractInsert implements SqlScript{
	
	/** 数据库表 */
	private SqlTable table;

	/** 插入数据的列 */
	private List<SqlColumn> columns;
	
	/**插入的值 */
	private List<String> values;
	
	/**
	 * 构造函数
	 * @param tableName 表
	 */
	public Insert(String tableName){
		this(new SqlTable(tableName));
	}
	
	/**
	 * 构造函数
	 * @param schema 数据库结构
	 * @param tableName 表名称
	 */
	public Insert(String schema ,String tableName){
		this(new SqlTable(schema,tableName));
	}
	
	/**
	 * 构造函数
	 * @param table 数据库表
	 */
	public Insert(SqlTable table){
		this.table = table;
		columns = new ArrayList<SqlColumn>();
		values = new ArrayList<String>();
	}
	
	/**
	 * 设定数据库结构
	 * @param schema
	 */
	public void setSchema(String schema){
		this.table.setSchema(schema);
	}
	
	/**
	 * 获取数据结构
	 * @return
	 */
	public String getSchema(){
		return this.table.getSchema();
	}
	
	/**
	 * 获取数据库名称
	 * @return
	 */
	public String getTableName(){
		return this.table.getTableName();
	}
	
	/**
	 * 添加插入列,插入列要包含数据类型,否则按照字符处理.
	 * @param columns
	 */
	@Deprecated
	public Insert addColumn(SqlColumn ...columns ){
		for(SqlColumn column : columns ){
			if( column != null )
				this.columns.add(column);
		}
		return this;
	}
	
	/**
	 * 添加插入值,值顺序和列的顺序要一致
	 * @param values
	 */
	@Deprecated
	public Insert addValue(String ...values ){
		for(String value : values ){
			if( values != null )
				this.values.add(value);
		}
		return this;
	}
	
	/**
	 * 添加需要插入的数据列
	 * @param column 列
	 * @param value 插入值
	 */
	public Insert addInserColumn(SqlColumn column,  String value){
		if(column != null && value != null){
			this.columns.add(column);
			this.values.add(value);
		}
		
		return this;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.dom.sql.dml.AbstractInsert#compiledContent(int)
	 */
	@Override
	public CharacterBuilder compiledContent(int indentLevel ) throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder();
		
		int columnsSize = columns.size();
		int valueSize = values.size();
		if(valueSize>0 ){
			answer.append(super.compiledContent(indentLevel))
			.append(table.compiledContent(0));
			
			if( columnsSize > 0 ){
				CharacterBuilder column = CharacterBuilderFactory.createC16StringBuilder();
				CharacterBuilder value = CharacterBuilderFactory.createC16StringBuilder();
				boolean comma = false;
				for( int i=0;i < columnsSize;i++ ){
					if (comma){column.append(SqlKeyWord.OPERATION_COMMA);value.append(SqlKeyWord.OPERATION_COMMA);}else comma = true;
					IndentSpace.newSpace(column, indentLevel+1);
					SqlColumn c = columns.get(i);
					column.append(c.getName());
					boolean b = !c.isNumber();
					if( i < valueSize){
						IndentSpace.newSpace(value, indentLevel+1);	
						if( b )value.append(SqlKeyWord.OPERATION_SINGLE_QUOTES);
						value.append(values.get(i));
						if( b )value.append(SqlKeyWord.OPERATION_SINGLE_QUOTES);
					}else{
						value.append("NULL");
					}
				}
				answer.append(SqlKeyWord.OPERATION_PAREN_LEFT)
				.append(column)
				.append(SqlKeyWord.OPERATION_PAREN_RIGHT);
				IndentSpace.newSpace(answer, indentLevel);
				answer.append(SqlKeyWord.VALUES)
				.append(SqlKeyWord.OPERATION_PAREN_LEFT)
				.append(value)
				.append(SqlKeyWord.OPERATION_PAREN_RIGHT)
				.append(SqlKeyWord.OPERATION_SEMICOLON);
			}else{
				IndentSpace.newSpace(answer, indentLevel );
				answer.append(SqlKeyWord.VALUES)
				.append(SqlKeyWord.OPERATION_PAREN_LEFT)
				;
				boolean comma = false;
				for(String value: values ){
					if (comma){answer.append(SqlKeyWord.OPERATION_COMMA);}else comma = true;
					IndentSpace.newSpace(answer, indentLevel+1);	
					answer.append(SqlKeyWord.OPERATION_SINGLE_QUOTES)
					.append(value)
					.append(SqlKeyWord.OPERATION_SINGLE_QUOTES)
					;
				}
				answer
				.append(SqlKeyWord.OPERATION_PAREN_RIGHT)
				.append(SqlKeyWord.OPERATION_SEMICOLON)
				;
			}
		}
		return answer;
	}
}
